package utis;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo3Util {
    private String url="src/main/resources/mybatis-config.xml";

    //加载
    public Document loadXml(){
        try {
            // 1、得到DOM解析器的工厂实例
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            // 2、从DOM工厂获得DOM解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            // 3、解析XML文档，得到一个Document，即DOM树
            Document doc = db.parse(url);
            return doc;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Test
    //查看
    public void test(){
        Document doc = loadXml();
        NodeList nodeList = doc.getElementsByTagName("dataSource");
        for (int i=0;i<nodeList.getLength();i++){
            Element exception = (Element) nodeList.item(i);
            String dataSource = exception.getAttribute("type");
            System.out.println(dataSource);
        }
        NodeList nodeList1 = doc.getElementsByTagName("property");
        for (int i=0; i<nodeList1.getLength();i++){
            Element element = (Element) nodeList1.item(i);
            String propertyName = element.getAttribute("name");
            String propertyValue = element.getAttribute("value");
            System.out.print(propertyName);
            System.out.print("\t"+propertyValue+"\n");
        }
    }
    @Test
    //修改
    public void update(){
        Document doc = loadXml();
        NodeList nodeList = doc.getElementsByTagName("dataSource");
        for (int i=0;i<nodeList.getLength();i++){
            Element element = (Element) nodeList.item(i);
            if (element.getAttribute("type").equals("POOLED")){
                element.setAttribute("type","POOLED");
            }
        }
        //保存
        saveInfo(doc);
    }
    @Test
    //新增
    public void insert(){
        Document doc = loadXml();
        Element element1 = (Element) doc.getElementsByTagName("dataSource").item(0);
        Element element = doc.createElement("property");
        element.setAttribute("name","password1");
        element.setAttribute("value","12321312321");
        element1.appendChild(element);
        //保存
        saveInfo(doc);
    }

    @Test
    //删除
    public void delete(){
        Document doc = loadXml();
        NodeList nodeList = doc.getElementsByTagName("property");
        for (int i=0;i<nodeList.getLength();i++){
            Element element = (Element) nodeList.item(i);
            if (element.getAttribute("name").equals("password1")){
                element.getParentNode().removeChild(element);
            }
        }
        //保存
        saveInfo(doc);
    }

    //保存XML
    public void saveInfo(Document doc ){
        //1.创建转换工厂
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            //2.创建转换器
            Transformer former = factory.newTransformer();
            DOMSource xmlSource = new DOMSource(doc);
            StreamResult outputTarget = new StreamResult(new FileOutputStream(url));
            //3.设置编码类型
            former.setOutputProperty(OutputKeys.ENCODING, "gb2312");
            //4.把DOM树转换为XML文件
            former.transform(xmlSource, outputTarget);
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
